package com.itfreer.ui.base.datetime;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itfreer.ui.R;
import com.itfreer.ui.base.api.IEditTextValidator;
import com.itfreer.ui.base.api.IPower;
import com.itfreer.ui.base.api.ISimpleView;

import java.util.Calendar;

/**
 * 定义日期选择控件
 */
public class DatePickerView extends LinearLayout
        implements DatePickerDialog.OnDateSetListener, View.OnClickListener, ISimpleView, IEditTextValidator, IPower {

    private TextView textView = null;
    public DatePickerDialog datePickerDialog;
    private String fieldName;
    private Object fieldValue;
    private String securityID;
    private boolean securityVisible = true;
    private String validatorName;
    private String validateTitle;
    private boolean readonly = false;
    private boolean required = false;
    private String textGravity = "";

    private OnDateSetListener mCallBack;

    /**
     * The callback used to indicate the user is done filling in the date.
     */
    public interface OnDateSetListener {
        void onDateSet(String fieldName, int startYear, int startMonthOfYear, int startDayOfMonth);
    }

    public DatePickerView(Context context) {
        super(context);
        setView(context);
    }

    public DatePickerView(Context context, AttributeSet attrs){
        super(context, attrs);
        getAttrs(context, attrs);
        setView(context);
    }

    public DatePickerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
        setView(context);
    }

    protected void getAttrs(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleView);
        this.fieldName = a.getString(R.styleable.SimpleView_fieldName);
        securityID = a.getString(R.styleable.SimpleView_securityID);
        validatorName = a.getString(R.styleable.SimpleView_validatorName);
        validateTitle = a.getString(R.styleable.SimpleView_validateTitle);
        this.required = a.getBoolean(R.styleable.SimpleView_required, false);
        this.readonly = a.getBoolean(R.styleable.SimpleView_readonly, false);
        this.textGravity = a.getString(R.styleable.SimpleView_textGravity);
    }

    /**
     * 设置当前的视图
     * @param context
     */
    protected void setView(Context context){
        if (context instanceof OnDateSetListener) {
            mCallBack = (OnDateSetListener) context;
        }

        View view = View.inflate(context, R.layout.com_itfreer_core_ui_base_inputitem, null);

        textView = (TextView)view.findViewById(R.id.com_itfreer_core_ui_base_datepickerview_textview);
        if(textGravity!=null) {
            if(this.textGravity.equals("right")) {
                textView.setGravity(Gravity.RIGHT);
            } else if(this.textGravity.equals("center")) {
                textView.setGravity(Gravity.CENTER);
            }
        }
        setReadonly(readonly);

        Calendar c = Calendar.getInstance();
        datePickerDialog =  new DatePickerDialog(DatePickerView.this.getContext(), 0, DatePickerView.this,
                c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), true);

        LayoutParams param =
                new LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(view, param);
    }

    @Override
    public void onClick(View v) {
        // 最后一个false表示不显示日期，如果要显示日期，最后参数可以是true或者不用输入
        datePickerDialog.show();
    }

    @Override
    public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear,
                          int startDayOfMonth) {
        fieldValue = String.format("%d/%d/%d", startYear, startMonthOfYear + 1, startDayOfMonth);
        textView.setText((String) fieldValue);
        if (mCallBack != null) {
            mCallBack.onDateSet(fieldName, startYear, startMonthOfYear, startDayOfMonth);
        }

    }

    /**
     * 设置绑定的依据对象
     * @param name
     */
    @Override
    public void setFieldName(String name){
        this.fieldName = name;
    }

    /**
     * 获取绑定的依据对象
     * @return
     */
    @Override
    public String getFieldName(){
        return fieldName;
    }

    /**
     * 设置绑定的值
     * @param value
     */
    @Override
    public void setFieldValue(Object value){
        this.fieldValue = value;

        if(fieldValue==null){
            return;
        }

        String strValue = fieldValue.toString();
        String[] values = strValue.split("/");
        if(values.length!=3){
            return;
        }

        textView.setText((String)fieldValue);

        int year = Integer.parseInt(values[0]);
        int month = Integer.parseInt(values[1]) - 1;
        int day = Integer.parseInt(values[2]);

        datePickerDialog.updateDate(year,month,day);
    }

    /**
     * 获取当前的值
     * @return
     */
    @Override
    public Object getFieldValue(){
        return fieldValue;
    }

    /**
     * 获取权限ID
     * @return
     */
    public String getSecurityID(){
        return securityID;
    }

    /**
     * 没有权限时，设置是否可见
     * @param visible
     */
    public void setSecurityVisible(boolean visible){
        securityVisible = visible;
        if(visible){
            this.setVisibility(View.VISIBLE);
        }else{
            // 表示不可见并且不占用空间
            this.setVisibility(View.GONE);
        }
    }

    /**
     * 获取验证标题
     * @return
     */
    public String getValidateTitle(){
        return validateTitle;
    }

    /**
     * 设置验证标题
     * @param validateTitle
     */
    public void setValidateTitle(String validateTitle){
        this.validateTitle = validateTitle;
    }

    /**
     * 获取验证名称
     * @return
     */
    public String getValidatorName(){
        return validatorName;
    }

    /**
     * 设置验证名称
     * @param name
     */
    public void setValidatorName(String name){
        validatorName = name;
    }

    public void setReadonly(boolean readonly) {
        this.readonly = readonly;
        if(readonly) {
            textView.setOnClickListener(null);
            textView.setTextColor(Color.parseColor("#c1c1c1"));
        } else {
            textView.setOnClickListener(this);
            textView.setTextColor(Color.parseColor("#000000"));
        }
    }

    public void setTextGravity(String textGravity) {
        this.textGravity = textGravity;
        if(textGravity!=null) {
            if(this.textGravity.equals("right")) {
                textView.setGravity(Gravity.RIGHT);
            } else if(this.textGravity.equals("center")) {
                textView.setGravity(Gravity.CENTER);
            }
        }
    }
}